package cn.dida.util;

/**
 * Created by zhangzhiling on 2017/8/10.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 根据一段时间区间，按月份拆分成多个时间段
 * @author lxg
 *
 * 2016年9月5日下午6:18:36
 */
public class SplitDateUtil {


//    public void demo(){
//        List<KeyValueForDate> list = SplitDateUtil.getKeyValueForDate("2015-08-23","2016-06-10");
//        System.out.println("开始日期--------------结束日期");
//        for(KeyValueForDate date : list){
//            System.out.println(date.getStartDate()+"-----"+date.getEndDate());
//        }
//    }


    /**
     * 根据一段时间区间，按月份拆分成多个时间段
     * @param startDate 开始日期
     * @param endDate  结束日期
     * @return
     */
    @SuppressWarnings("deprecation")
    public static List<KeyValueForDate> splitForDate(String startDate,String endDate) {
        List<KeyValueForDate> list = null;
        try {
            list = new ArrayList<KeyValueForDate>();

            Date firstDay;
            Date lastDay;
            Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);// 定义起始日期

            Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);// 定义结束日期

            Calendar dd = Calendar.getInstance();// 定义日期实例
            dd.setTime(d1);// 设置日期起始时间
            Calendar cale = Calendar.getInstance();

            Calendar c = Calendar.getInstance();
            c.setTime(d2);

            int startDay = d1.getDate();
            int endDay = d2.getDate();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            KeyValueForDate keyValueForDate = null;

            while (dd.getTime().before(d2)) {// 判断是否到结束日期
                keyValueForDate = new KeyValueForDate();
                cale.setTime(dd.getTime());

                if(dd.getTime().equals(d1)){
                    cale.set(Calendar.DAY_OF_MONTH, dd
                            .getActualMaximum(Calendar.DAY_OF_MONTH));
                    lastDay = cale.getTime();
                    keyValueForDate.setStartDate(DateUtils.getTheDayMin(d1).getTime());
                    keyValueForDate.setEndDate(DateUtils.getTheDayMax(lastDay).getTime());

                }else if(dd.get(Calendar.MONTH) == d2.getMonth() && dd.get(Calendar.YEAR) == c.get(Calendar.YEAR)){
                    cale.set(Calendar.DAY_OF_MONTH,1);//取第一天
                    firstDay = cale.getTime();

                    keyValueForDate.setStartDate(DateUtils.getTheDayMin(firstDay).getTime());
                    keyValueForDate.setEndDate(DateUtils.getTheDayMax(d2).getTime());

                }else {
                    cale.set(Calendar.DAY_OF_MONTH,1);//取第一天
                    firstDay = cale.getTime();

                    cale.set(Calendar.DAY_OF_MONTH, dd
                            .getActualMaximum(Calendar.DAY_OF_MONTH));
                    lastDay = cale.getTime();

                    keyValueForDate.setStartDate(DateUtils.getTheDayMin(firstDay).getTime());
                    keyValueForDate.setEndDate(DateUtils.getTheDayMax(lastDay).getTime());

                }
                list.add(keyValueForDate);
                dd.add(Calendar.MONTH, 1);// 进行当前日期月份加1

            }

            if(endDay<startDay){
                keyValueForDate = new KeyValueForDate();

                cale.setTime(d2);
                cale.set(Calendar.DAY_OF_MONTH,1);//取第一天
                firstDay = cale.getTime();

                keyValueForDate.setStartDate(DateUtils.getTheDayMin(firstDay).getTime());
                keyValueForDate.setEndDate(DateUtils.getTheDayMax(d2).getTime());
                list.add(keyValueForDate);
            }
        } catch (ParseException e) {
            return null;
        }

        return list;
    }

    /**
     * 按天分割时间段
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<KeyValueForDate> splitForDay(String startDate,String endDate) {

        List<KeyValueForDate> list = new ArrayList<>();
        Calendar startCanlandar = Calendar.getInstance();//开始时间
        Calendar endCanlandar = Calendar.getInstance();//结束时间
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            Date sd1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);// 定义起始日期
            Date ed2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);// 定义结束日期

            startCanlandar.setTime(sd1);//2009-6-1
            endCanlandar.setTime(ed2);//2009-6-5

            List<Date> returnList = new ArrayList<Date>();
            while(startCanlandar.compareTo(endCanlandar) < 1){

                KeyValueForDate deyDate = new KeyValueForDate();
                deyDate.setStartDate(DateUtils.getTheDayMin(startCanlandar.getTime()).getTime());
                deyDate.setEndDate(DateUtils.getTheDayMax(startCanlandar.getTime()).getTime());
               list.add(deyDate);
                startCanlandar.add(startCanlandar.DATE, 1);//每次循环增加一天
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

        return list;

    }

}


